## This file makes all figures pertaining to the simulations

# This file requires "sim/out/performance.rda" to be loaded beforehand


## Omit J=20
perf20 <- list()
for(i in 1:length(performance_df)){
  perf20[[i]] <- performance_df[[i]][performance_df[[i]]$J!=20,]
}

buf <- .25; botbuf <- 1 # Buffer between graphs
label_size <- 24; text_size <- 30; title_size <- 34
titles <- c("Probabilistic DGP", "Deterministic DGP")
vshapes <- c(16,17)
vlines <- c("solid", "longdash")
vcol <- c("black", "grey")
nvar <- unique(perf20[[1]]$J)
g <- list()
size <- 2; mult <- 3
ylim_num <- c(0, max(c(perf20[[1]]$nrules, perf20[[2]]$nrules)))
ylim_len <- c(0, max(c(perf20[[1]]$lenrules, perf20[[2]]$lenrules)))
ylim_acc <- c(0, max(c(perf20[[1]]$acc, perf20[[2]]$acc)))
plegend <- ggplot(perf20[[1]], aes(x=n, y=acc, color=method, shape=method))+
  geom_line()+
  scale_linetype_manual(values=vlines)+
  geom_point()+
  scale_shape_manual(values=vshapes)+
  scale_color_manual(values=vcol)+
  theme_bw()+
  theme(axis.text=element_text(size=label_size), axis.title=element_text(size=text_size), 
        legend.title = element_text(size = text_size),legend.text = element_text(size = text_size),
        plot.margin=unit(c(0,0,0,0),"cm"))
legend <- cowplot::get_legend(plegend)
for(i in 1:length(perf20)){
  performance <- perf20[[i]]
  for(j in 1:length(nvar)){
    index <- (i-1)*length(nvar)+j
    perfj <- performance[performance$J==nvar[j], ]
    p1 <- ggplot(perfj, aes(x=n, y=nrules, color=method, shape=method, linetype=method))+
      geom_line(size=size)+
      geom_point(size=size*mult)+
      scale_linetype_manual(values=vlines)+
      scale_shape_manual(values=vshapes)+
      scale_color_manual(values=vcol)+
      ylab("Average Number of Rules")+
      xlab("Sample Size")+
      ylim(ylim_num)+
      theme_bw()+
      theme(axis.text=element_text(size=label_size), axis.title=element_text(size=text_size), legend.position = "none", 
            plot.margin=unit(c(buf,0,botbuf,2*buf),"cm"))
    # Length of rules 
    p2 <- ggplot(perfj, aes(x=n, y=lenrules, color=method, shape=method, linetype=method))+
      geom_line(size=size)+
      geom_point(size=size*mult)+
      scale_linetype_manual(values=vlines)+
      scale_shape_manual(values=vshapes)+
      scale_color_manual(values=vcol)+
      ylab("Average Length of Rules")+
      xlab("Sample Size")+
      ylim(ylim_len)+
      theme_bw()+
      theme(axis.text=element_text(size=label_size), axis.title=element_text(size=text_size), legend.position = "none", 
            plot.margin=unit(c(buf,0,botbuf,2*buf),"cm"))
    # Accuracy
    p3 <- ggplot(perfj, aes(x=n, y=acc, color=method, shape=method, linetype=method))+
      geom_line(size=size)+
      geom_point(size=size*mult)+
      scale_linetype_manual(values=vlines)+
      scale_shape_manual(values=vshapes)+
      scale_color_manual(values=vcol)+
      ylab("Average Accuracy")+
      xlab("Sample Size")+
      ylim(ylim_acc)+
      theme_bw()+
      theme(axis.text=element_text(size=label_size), axis.title=element_text(size=text_size), legend.position = "none", 
            plot.margin=unit(c(buf,0,botbuf,2*buf),"cm"))
    title <- paste(titles[i], ", ", nvar[j], " variables", sep="")
    g[[index]] <- grid.arrange(p1,p2,p3,legend,nrow=1, widths=c(4,4,4,1),
                               top=textGrob(title, gp=gpar(fontsize=title_size,font=3)))
  }
}
png("figures/sim_rows.png", height=8000, width=8000, res=300)
grid.arrange(g[[3]], g[[4]], g[[1]], g[[2]],  ncol=1)
dev.off()


## J=20
g20 <- list()
ylim_num <- 0; ylim_lem <- 0; ylim_acc <- 0;
for(i in 1:length(performance_df)){
  performance <- performance_df[[i]]
  perfj <- performance[performance$J==20, ]
  ylim_num <- c(0, max(c(ylim_num, perfj$nrules, perfj$nrules), na.rm=T))
  ylim_len <- c(0, max(c(ylim_len, perfj$lenrules, perfj$lenrules), na.rm=T))
  ylim_acc <- c(0, max(c(ylim_acc, perfj$acc, perfj$acc), na.rm=T))
}
for(i in 1:length(performance_df)){
  performance <- performance_df[[i]]
  perfj <- performance[performance$J==20, ]
  ydisplay <- i
  p1 <- ggplot(perfj, aes(x=n, y=nrules, color=method, shape=method, linetype=method))+
    geom_line(size=size)+
    geom_point(size=size*mult)+
    scale_linetype_manual(values=vlines)+
    scale_shape_manual(values=vshapes)+
    scale_color_manual(values=vcol)+
    ylab(c("Average Number of Rules", "")[ydisplay])+
    xlab("")+
    ylim(ylim_num)+
    theme_bw()+
    theme(axis.text=element_text(size=14), axis.title=element_text(size=24), legend.position = "none")
  # Length of rules 
  p2 <- ggplot(perfj, aes(x=n, y=lenrules, color=method, shape=method, linetype=method))+
    geom_line(size=size)+
    geom_point(size=size*mult)+
    scale_linetype_manual(values=vlines)+
    scale_shape_manual(values=vshapes)+
    scale_color_manual(values=vcol)+
    ylab(c("Average Length of Rules", "")[ydisplay])+
    xlab("")+
    ylim(ylim_len)+
    theme_bw()+
    theme(axis.text=element_text(size=14), axis.title=element_text(size=24), legend.position = "none")
  p3 <- ggplot(perfj, aes(x=n, y=acc, color=method, shape=method, linetype=method))+
    geom_line(size=size)+
    geom_point(size=size*mult)+
    scale_linetype_manual(values=vlines)+
    scale_shape_manual(values=vshapes)+
    scale_color_manual(values=vcol)+
    ylab(c("Average Accuracy", "")[ydisplay])+
    xlab("Sample Size")+
    ylim(ylim_acc)+
    theme_bw()+
    theme(axis.text=element_text(size=14), axis.title=element_text(size=24), legend.position = "none")
  title <- paste(titles[i], ", ", 20, " variables", sep="")
  g20[[i]] <- grid.arrange(p1,p2,p3,ncol=1,top=textGrob(title, gp=gpar(fontsize=28,font=3)))
}
glegend <- grid.arrange(legend, legend, legend, ncol=1)
png("figures/sim20.png", height=4500, width=4500, res=300)
grid.arrange(g20[[1]], g20[[2]], glegend, ncol=3, widths=c(3,3,1))
dev.off()
